package com.chuangke.flow.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.flow.dao.mapper.FlowRuFlowMapper;
import com.chuangke.flow.entity.FlowRuFlow;
import com.chuangke.flow.enums.FlowNodeStatusEnum;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author chuangke
 * @date "2020-09-27"
 */
@Service
public class FlowRuFlowDao extends ServiceImpl<FlowRuFlowMapper, FlowRuFlow> {

	public void insertBatchSomeColumn(Collection<FlowRuFlow> entityList) {
		if (CollectionUtils.isEmpty(entityList)) {
			return;
		}
		baseMapper.insertBatchSomeColumn(entityList);
	}

	/**
	 * 代替FlowRuFlowMapper.selectFlowByStatus
	 * 
	 * @param pageQuery
	 * @return
	 */
	public IPage<FlowRuFlow> findTodoTask(CkPageQuery pageQuery) {
		QueryWrapper<FlowRuFlow> qw = getTodoQueryWrapper(pageQuery);
		return this.page(pageQuery.getPage(), qw);
	}

	/**
	 * 代替FlowRuFlowMapper.selectFlowCountByStatus
	 * 
	 * @param pageQuery
	 * @return
	 */
	public long findTodoTaskCount(HashMap<String, Object> parms) {
		return this.count(getTodoQueryWrapper(parms));
	}

	private QueryWrapper<FlowRuFlow> getTodoQueryWrapper(HashMap<String, Object> parms) {
		String userId = (String) parms.get("userId");
		@SuppressWarnings("unchecked")
		List<String> orderTypes = (List<String>) parms.get("orderTypes");

		QueryWrapper<FlowRuFlow> qw = new QueryWrapper<>();
		if (CollectionUtils.isNotEmpty(orderTypes)) {
			qw.lambda().in(FlowRuFlow::getOrderType, orderTypes);
		}
		String insql = "select flow_id from flow_ru_task where flow_ru_task.user_id='" + userId
				+ "' and flow_ru_task.status='" + FlowNodeStatusEnum.unhandle.getCode() + "'";
		qw.lambda().inSql(FlowRuFlow::getId, insql);
		qw.lambda().orderByDesc(FlowRuFlow::getCreatedAt);
		return qw;
	}

}
